package com.learn.join;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * @create: 2023-04-24 17:17
 * @author: Mr.Du
 * --------------
 * @notes:
 **/
public class TumblingWindowIntervalJoinDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> mainOrderSource = env.socketTextStream("node1", 9999);
        DataStreamSource<String> detailOrderSource = env.socketTextStream("node1", 9998);


        SingleOutputStreamOperator<MainOrder> mainOrderStream = mainOrderSource.map(new MapFunction<String, MainOrder>() {
            @Override
            public MainOrder map(String s) throws Exception {
                String[] arr = s.split(",");
                return new MainOrder(arr[0], arr[1], arr[2], arr[3], Double.parseDouble(arr[4]));
            }
        }).assignTimestampsAndWatermarks(WatermarkStrategy.<MainOrder>forBoundedOutOfOrderness(Duration.ZERO)
                .withTimestampAssigner(new SerializableTimestampAssigner<MainOrder>() {
                    @Override
                    public long extractTimestamp(MainOrder mainOrder, long l) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            return format.parse(mainOrder.getOrderTime()).getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                            return 0L;
                        }
                    }
                }));

        SingleOutputStreamOperator<DetailOrder> detailOrderStream = detailOrderSource.map(new MapFunction<String, DetailOrder>() {
            @Override
            public DetailOrder map(String s) throws Exception {
                String[] arr = s.split(",");
                return new DetailOrder(arr[0], arr[1], arr[2], arr[3], Integer.parseInt(arr[4]), Double.parseDouble(arr[5]));
            }
        }).assignTimestampsAndWatermarks(WatermarkStrategy.<DetailOrder>forBoundedOutOfOrderness(Duration.ZERO)
                .withTimestampAssigner(new SerializableTimestampAssigner<DetailOrder>() {
                    @Override
                    public long extractTimestamp(DetailOrder detailOrder, long l) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            return format.parse(detailOrder.getDetailTime()).getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                            return 0L;
                        }
                    }
                }));

        mainOrderStream
                .keyBy(MainOrder::getOrderId)
                .intervalJoin(detailOrderStream.keyBy(DetailOrder::getOrderId))
                .between(Time.seconds(-5), Time.seconds(5))
                .upperBoundExclusive()
                .process(new ProcessJoinFunction<MainOrder, DetailOrder, DwdOrder>() {
                    @Override
                    public void processElement(MainOrder mainOrder, DetailOrder detailOrder, ProcessJoinFunction<MainOrder, DetailOrder, DwdOrder>.Context context, Collector<DwdOrder> out) throws Exception {
                        DwdOrder dwdOrder = new DwdOrder(mainOrder.getOrderTime(), mainOrder.getOrderId(),
                                mainOrder.getUserId(), mainOrder.getAddress(), mainOrder.getOrderMoney(),
                                detailOrder.getDetailTime(), detailOrder.getDetailId(),
                                detailOrder.getGoodsName(), detailOrder.getGoodsNumber(), detailOrder.getDetailMoney());
                        out.collect(dwdOrder);
                    }
                }).print();

        env.execute();
    }
}
